<?php

/**
 * This is the model class for table "{{product_channel}}".
 *
 * The followings are the available columns in table '{{product_channel}}':
 * @property string $id
 * @property string $level
 * @property string $parent_id
 * @property string $title
 * @property string $sort
 * @property integer $visible
 */
class ProductChannel extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return ProductChannel the static model class
	 */
	public static function model($className=__CLASS__) {return parent::model($className);}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {return '{{product_channel}}';}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// Define only rules for those attributes that will receive user inputs.
		return array(
			array('title', 'required'),
			array('visible', 'numerical', 'integerOnly'=>true),
			array('level, parent_id, sort', 'length', 'max'=>10),
			array('title', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, level, parent_id, title, sort, visible', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
			'parent'=>array(self::BELONGS_TO,'ProductChannel','parent_id'),
			'children'=>array(self::HAS_MANY,'ProductChannel','parent_id'),
			'posts'=>array(self::HAS_MANY,'ProductPost','channel_id'),
			'countChildren'=>array(self::STAT,'ProductChannel','parent_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' 	=> Yii::t('productchannel', 'ID'),
			'level' 	=> Yii::t('productchannel', 'Cấp'),
			'parent_id' 	=> Yii::t('productchannel', 'Cấp 1'),
			'title' 	=> Yii::t('productchannel', 'Chủ đề'),
			'sort' 	=> Yii::t('productchannel', 'Thứ tự'),
			'visible' 	=> Yii::t('productchannel', 'Ẩn/Hiện'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($cri=null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id,true);
		$criteria->compare('t.level',$this->level,true);
		$criteria->compare('t.parent_id',$this->parent_id,true);
		$criteria->compare('t.title',$this->title,true);
		$criteria->compare('t.sort',$this->sort,true);
		$criteria->compare('t.visible',$this->visible);
		if($cri!==null) $criteria->mergeWith($cri);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array('defaultOrder'=>'t.sort ASC, t.id DESC'),
			'pagination'=>array('pageSize'=>Yii::app()->session->get('backendPageSize',25)),
		));
	}

	public function findAllChannelLevel($level)
	{
		$criteria = new CDbCriteria; 
		$criteria->condition = 'level=:level';			
		$criteria->params = array(':level'=>$level);
		return self::model()->findAll($criteria);
	}

	public function getUrl() {
		return Yii::app()->createAbsoluteUrl('/produtc/product/listProduct', array('channel_id'=>$this->id));
	}
}